<?php
    function resize_jpg($path, $dest, $size_x=200, $size_y=200, $quality=80, $bestfit=TRUE, $sizeUp=TRUE){
    	//Resize uploaded file
    	$orig;
    	
    	$img_type = exif_imagetype($path);
    	if($img_type == IMAGETYPE_JPEG ){
			$orig = imagecreatefromjpeg($path);
    	}elseif($img_type == IMAGETYPE_GIF ){
    		$orig = imagecreatefromgif($path);
    	}elseif($img_type == IMAGETYPE_PNG ){
    		$orig = imagecreatefrompng($path);
    	}elseif($img_type == IMAGETYPE_WBMP ){
    		$orig = imagecreatefromwbmp($path);
    	}else{
    		return false;
    	}
		$orig_x = imagesx($orig);
		$orig_y = imagesy($orig);
		
		if( $sizeUp || ($orig_x > $size_x || $orig_y > $size_y) ){
			//Scale target
			$scale_x = $size_x;
			$scale_y = $size_y;
			
			if( $bestfit ){
				if( $orig_x > $orig_y ){
					$scale_y = round($scale_x * ($orig_y/$orig_x), 0);
				}elseif( $orig_x < $orig_y ){
					$scale_x = round( $scale_y * ($orig_x / $orig_y),0);
				}
			}
			//echo "X: " . $scale_x . "<br>Y: " . $scale_y;exit;
			$image = imagecreatetruecolor($scale_x, $scale_y);
	        imagecopyresampled($image, $orig, 0, 0, 0, 0, $scale_x, $scale_y, $orig_x, $orig_y);
			
	        if( imagejpeg($image, $dest, $quality) ){
				imagedestroy($image);
				return TRUE;	
			}else{
				throw new Exception("Error resizing image.", "000003");
				imagedestroy($image);
				return FALSE;		
			}
		}else{ //its a small image that doesn't want to be scaled up, just move it.
			return move_uploaded_file($path, $dest);
		}
		
    }
?>